﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCOChart2
{
    public partial class FormAntdTable : Form
    {
        public FormAntdTable()
        {
            InitializeComponent();

            table1.Columns = new AntdUI.ColumnCollection
            {
                new AntdUI.ColumnCheck("check").SetFixed(),
                new AntdUI.Column("name", "姓名").SetFixed().SetLocalizationTitleID("Table.Column."),
                new AntdUI.ColumnCheck("checkTitle", "不全选标题").SetColAlign().SetLocalizationTitleID("Table.Column."),
                new AntdUI.ColumnRadio("radio", "单选").SetLocalizationTitleID("Table.Column."),
                new AntdUI.Column("online", "状态", AntdUI.ColumnAlign.Center).SetLocalizationTitleID("Table.Column."),
                new AntdUI.ColumnSwitch("enable", "启用", AntdUI.ColumnAlign.Center)
                {
                    LocalizationTitle ="Table.Column.{id}",
                    Call = (value, record, i_row, i_col) => {
                        System.Threading.Thread.Sleep(2000);
                        //AntdUI.Message.success(this,"qiy");
                        //show();  //调用函数
                        return value;
                    }
                },
                new AntdUI.Column("age", "年龄", AntdUI.ColumnAlign.Center).SetLocalizationTitleID("Table.Column."),
                new AntdUI.Column("address", "住址").SetLocalizationTitleID("Table.Column."),
                new AntdUI.Column("tag", "Tag"),
                new AntdUI.Column("imgs", "图片").SetLocalizationTitleID("Table.Column."),
                new AntdUI.Column("btns", "操作").SetFixed().SetWidth("auto").SetLocalizationTitleID("Table.Column."),
            };

            table1.DataSource = GetPageData(pagination1.Current, pagination1.PageSize);
            pagination1.PageSizeOptions = new int[] { 10, 20, 30, 50, 100 };
        }

        #region 示例
        private void checkFixedHeader_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            table1.FixedHeader = e.Value;

        }


        void checkColumnDragSort_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            table1.ColumnDragSort = e.Value;
            
        }

        void checkRowsDragSort_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (e.Value)
            {
                table1.Columns.Insert(0, new AntdUI.ColumnSort() { Fixed = false });  // bug
            }
            else { 
                table1.Columns.RemoveAt(0); 
            }
        }

        void checkBordered_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            table1.Bordered = e.Value;
            
        }

        #region 奇偶列

        void checkSetRowStyle_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (e.Value) table1.SetRowStyle += table1_SetRowStyle;
            else table1.SetRowStyle -= table1_SetRowStyle;
            table1.Invalidate();
        }
        AntdUI.Table.CellStyleInfo table1_SetRowStyle(object sender, AntdUI.TableSetRowStyleEventArgs e)
        {
            if (e.RowIndex % 2 == 0)
            {
                return new AntdUI.Table.CellStyleInfo
                {
                    BackColor = AntdUI.Style.Db.ErrorBg,
                    ForeColor = AntdUI.Style.Db.Error
                };
            }
            return null;
        }

        #endregion

        void checkSortOrder_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (table1.Columns != null) table1.Columns[6].SortOrder = table1.Columns[7].SortOrder = e.Value;
        }

        void checkEnableHeaderResizing_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            table1.EnableHeaderResizing = e.Value;
            
        }

        void checkVisibleHeader_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            table1.VisibleHeader = e.Value;
        }

        private void checkAddressLineBreak_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (e.Value) table1.Columns[7].Width = "120";
            else table1.Columns[7].Width = null;
            table1.Columns[7].LineBreak = e.Value;
            
        }


        #endregion

        #region 点击/双击
        void table1_CellClick(object sender, AntdUI.TableClickEventArgs e)
        {
            if (e.Record is TestClass data)
            {
                if (e.RowIndex > 0 && e.ColumnIndex == 6) AntdUI.Popover.open(new AntdUI.Popover.Config(table1, "演示一下能弹出自定义") { Offset = e.Rect });
                else if (e.RowIndex > 0 && e.ColumnIndex == 8)
                {
                    if (data.tag == null) data.tag = new AntdUI.CellTag[] { new AntdUI.CellTag("NICE", AntdUI.TTypeMini.Success), new AntdUI.CellTag("DEVELOPER", AntdUI.TTypeMini.Info) };
                    else
                    {
                        if (data.tag.Length == 1)
                        {
                            if (data.tag[0].Text == "ERROR") data.tag = new AntdUI.CellTag[] { new AntdUI.CellTag("NICE", AntdUI.TTypeMini.Success), new AntdUI.CellTag("DEVELOPER", AntdUI.TTypeMini.Info) };
                            else
                            {
                                data.tag[0].Type = AntdUI.TTypeMini.Error;
                                data.tag[0].Text = "ERROR";
                            }
                        }
                        else data.tag = new AntdUI.CellTag[] { new AntdUI.CellTag("DEVELOPER", AntdUI.TTypeMini.Info) };
                    }
                }
            }
        }

        void table1_CellButtonClick(object sender, AntdUI.TableButtonEventArgs e)
        {
            if (e.Record is TestClass data)
            {
                if (e.Btn.Id == "delete")
                {
                    if (AntdUI.Modal.open(new AntdUI.Modal.Config(this, "是否删除", new AntdUI.Modal.TextLine[] {
                    new AntdUI.Modal.TextLine(data.name,AntdUI.Style.Db.Primary),
                    new AntdUI.Modal.TextLine(data.address,6,AntdUI.Style.Db.TextSecondary)
                    }, AntdUI.TType.Error)
                    {
                        CancelText = null,
                        OkType = AntdUI.TTypeMini.Error,
                        OkText = "删除"
                    }) == DialogResult.OK)
                    {
                        table1.Spin("正在加载中...", config =>    // spin进度条
                        {
                            System.Threading.Thread.Sleep(1000);
                            for (int i = 0; i < 101; i++)
                            {
                                config.Value = i / 100F;
                                config.Text = "处理中 " + i + "%";
                                System.Threading.Thread.Sleep(20);
                            }
                            System.Threading.Thread.Sleep(1000);
                            config.Value = null;
                            config.Text = "请耐心等候...";
                            System.Threading.Thread.Sleep(2000);
                        }, () =>
                        {
                            System.Diagnostics.Debug.WriteLine("加载结束");
                        });
                    }
                }
                else if(e.Btn.Id == "edit")
                {
                    AntdUI.Popover.open(new AntdUI.Popover.Config(table1, $"{1}点击了edit按钮") { Offset = e.Btn.Rect });
                }
            }
        }
        #endregion

        #region 分页与数据

        object GetPageData(int current, int pageSize)
        {
            var list = new List<TestClass>(pageSize);
            int start = Math.Abs(current - 1) * pageSize;
            DateTime now = DateTime.Now;
            DateTime birthday_TigerHu = new DateTime(1983, 7, 4);
            DateTime birthday_DanielWu = new DateTime(1974, 9, 30);

            list.Add(new TestClass(start, 0, "胡彦斌", (int)Math.Round((now - birthday_TigerHu).TotalDays / 365)));
            list.Add(new TestClass(start + 1, 1, "吴彦祖", (int)Math.Round((now - birthday_DanielWu).TotalDays / 365))
            {
                tag = new AntdUI.CellTag[] { new AntdUI.CellTag("NICE", AntdUI.TTypeMini.Success), new AntdUI.CellTag("DEVELOPER", AntdUI.TTypeMini.Info) }
            });
            for (int i = 2; i < pageSize; i++)
            {
                int index = start + i;
                list.Add(new TestClass(index, i, "胡彦祖", 20 + index));
            }
            return list;
        }

        void pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            table1.DataSource = GetPageData(e.Current, e.PageSize);
        }
        string pagination1_ShowTotalChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            return $"{e.PageSize} / {e.Total}条 {e.PageTotal}页";
        }

        #endregion



        public void show()
        {
            MessageBox.Show("该功能已触发！");
        }
    }


    public class TestClass : AntdUI.NotifyProperty
    {
        public TestClass(int index, int start, string name, int age)
        {
            id = (index + 1);
            if (start == 1) _online = new AntdUI.CellBadge(AntdUI.TState.Success, "在线");
            else if (start == 2) _online = new AntdUI.CellBadge(AntdUI.TState.Processing, "处置");
            else if (start == 3) _online = new AntdUI.CellBadge(AntdUI.TState.Error, "离线");
            else if (start == 4) _online = new AntdUI.CellBadge(AntdUI.TState.Warn, "离线");
            else _online = new AntdUI.CellBadge(AntdUI.TState.Default, "常规");
            _name = name;
            _age = age;
            _address = "西湖区湖底公园" + id + "号";
            _enable = start % 2 == 0;
            if (start == 1)
            {
                _imgs = new AntdUI.CellImage[] {
                        //new AntdUI.CellImage(Properties.Resources.img1) { BorderWidth = 4, BorderColor = Color.BlueViolet },
                        //new AntdUI.CellImage(Properties.Resources.bg1)
                    };
            }

            if (start == 2)
            {
                _btns = new AntdUI.CellLink[] {
                        new AntdUI.CellButton("b1") {
                            BorderWidth = 1,
                            IconSvg = "SearchOutlined",
                            //IconHoverSvg = Properties.Resources.icon_like,
                            ShowArrow = true
                        },
                        new AntdUI.CellButton("b2") { ShowArrow = true },
                        new AntdUI.CellButton("b3") { Type = AntdUI.TTypeMini.Primary, IconSvg = "SearchOutlined" }
                    };
            }
            else if (start == 3)
            {
                _btns = new AntdUI.CellLink[] {
                        new AntdUI.CellButton("b1", "Border") { BorderWidth = 1 },
                        new AntdUI.CellButton("b2", "GhostBorder") { Ghost = true, BorderWidth = 1, ShowArrow = true, IsLink = true }
                    };
            }
            else if (start == 4)
            {
                _btns = new AntdUI.CellLink[] {
                        new AntdUI.CellButton("edit", "Edit", AntdUI.TTypeMini.Primary) { Ghost = true, BorderWidth = 1 },
                        new AntdUI.CellButton("delete", "Delete", AntdUI.TTypeMini.Error) { Ghost = true, BorderWidth = 1 }
                    };
            }
            else if (start == 5)
            {
                _btns = new AntdUI.CellLink[] {
                        new AntdUI.CellButton("edit","Edit",AntdUI.TTypeMini.Primary),
                        new AntdUI.CellButton("delete","Delete",AntdUI.TTypeMini.Error)
                    };
            }
            else _btns = new AntdUI.CellLink[] { new AntdUI.CellLink("delete", "Delete") };
        }

        public int id { get; set; }

        bool _check = false;
        public bool check
        {
            get => _check;
            set
            {
                if (_check == value) return;
                _check = value;
                OnPropertyChanged();
            }
        }

        bool _radio = false;
        public bool radio
        {
            get => _radio;
            set
            {
                if (_radio == value) return;
                _radio = value;
                OnPropertyChanged();
            }
        }

        bool _checkTitle = false;
        public bool checkTitle
        {
            get => _checkTitle;
            set
            {
                if (_checkTitle == value) return;
                _checkTitle = value;
                OnPropertyChanged();
            }
        }

        string _name;
        public string name
        {
            get => _name;
            set
            {
                if (_name == value) return;
                _name = value;
                OnPropertyChanged();
            }
        }

        AntdUI.CellBadge _online;
        public AntdUI.CellBadge online
        {
            get => _online;
            set
            {
                _online = value;
                OnPropertyChanged();
            }
        }


        bool _enable = false;
        public bool enable
        {
            get => _enable;
            set
            {
                if (_enable == value) return;
                _enable = value;
                OnPropertyChanged();
            }
        }

        int _age;
        public int age
        {
            get => _age;
            set
            {
                if (_age == value) return;
                _age = value;
                OnPropertyChanged();
            }
        }

        string _address;
        public string address
        {
            get => _address;
            set
            {
                if (_address == value) return;
                _address = value;
                OnPropertyChanged();
            }
        }

        AntdUI.CellTag[] _tag;
        public AntdUI.CellTag[] tag
        {
            get => _tag;
            set
            {
                _tag = value;
                OnPropertyChanged();
            }
        }

        AntdUI.CellImage[] _imgs;
        public AntdUI.CellImage[] imgs
        {
            get => _imgs;
            set
            {
                _imgs = value;
                OnPropertyChanged();
            }
        }

        AntdUI.CellLink[] _btns;
        public AntdUI.CellLink[] btns
        {
            get => _btns;
            set
            {
                _btns = value;
                OnPropertyChanged();
            }
        }
    }

}
